<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="/assets/lib/layui/css/layui.css" />
    <style>
      .layui-card {
        width: 1200px;
        margin: 100px auto;
      }
    </style>
  </head>
  <body>
    <div class="layui-card">
      <div class="layui-card-header">发布留言案例</div>
      <div class="layui-card-body">
        <form class="layui-form" action="">
          <div class="layui-form-item layui-inline">
            <label class="layui-form-label">请输入姓名</label>
            <div class="layui-input-block">
              <input type="text" name="uname" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" />
            </div>
          </div>
          <div class="layui-form-item layui-inline">
            <label class="layui-form-label">请输入性别</label>
            <div class="layui-input-block">
              <input type="text" name="sex" required lay-verify="required" placeholder="请输入性别" autocomplete="off" class="layui-input" />
            </div>
          </div>
          <div class="layui-form-item layui-inline">
            <label class="layui-form-label">请输入年龄</label>
            <div class="layui-input-block">
              <input type="text" name="age" required lay-verify="required" placeholder="请输入年龄" autocomplete="off" class="layui-input" />
            </div>
          </div>
          <div class="layui-form-item layui-inline">
            <button type="button" class="layui-btn btn-ok">提交</button>
          </div>
        </form>
        <table class="layui-table">
          <colgroup>
            <col />
            <col />
            <col />
            <col />
          </colgroup>
          <thead>
            <tr>
              <th>姓名</th>
              <th>性别</th>
              <th>年龄</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody></tbody>
        </table>
      </div>
    </div>

    <!-- 定义渲染模板 -->
    <script type="text/html" id="msg">
      {{each demo}}
      <tr>
        <td>{{$value.uname}}</td>
        <td>{{$value.age}}</td>
        <td>{{$value.sex}}</td>
        <td>
          <button type="button" class="layui-btn  layui-btn-danger layui-btn-xs btn-delete" id="{{$index}}">删除</button>
        </td>
      </tr>
      {{/each}}
    </script>
    <script src="/assets/lib/layui/layui.all.js"></script>
    <script src="/assets/lib/jquery.js"></script>
    <script src="/assets/lib/template-web.js"></script>

    <script>
      $(function () {
        const layer = layui.layer;
        $(".btn-ok").on("click", function () {
          // 1. 加载本地存储的数据
          let data = loadData();
          // 2. 收集用户输入的数据
          let obj = {
            uname: $("[name=uname]").val().trim(),
            sex: $("[name=sex]").val().trim(),
            age: $("[name=age]").val().trim()
          };
          if ($("[name=uname]").val().trim() == "" || $("[name=sex]").val().trim() == "" || $("[name=age]").val().trim() == "") {
            return layer.msg("请输入完整的信息！");
          }
          data.push(obj);
          $("[name=uname]").val("");
          $("[name=sex]").val("");
          $("[name=age]").val("");
          // 3. 将收集到的数据存到本地
          saveData(data);
          // 4. 渲染页面
          renderData();
        });

        renderData();

        // 删除操作
        $("tbody").on("click", ".btn-delete", function () {
          var data = loadData();
          var id = $(this).attr("id");
          data.splice(id, 1);
          saveData(data);
          renderData();
        });

        function loadData() {
          let data = localStorage.getItem("msg");
          return data ? JSON.parse(data) : [];
        }
        function saveData(data) {
          localStorage.setItem("msg", JSON.stringify(data));
        }
        function renderData() {
          let datas = loadData();
          let str = template("msg", { demo: datas });
          $("tbody").empty().html(str);
        }
      });
    </script>
  </body>
</html>
